#!/usr/bin/env bash

func_A()
{
        echo "A";
}

func_B()
{
        echo "B";
}

func()
{
    local v
    v="func"
    echo "func: v=$v"

    local t="cv"
    eval local $t
    eval $t=200
    echo "func: cv="$(eval echo \$$t)
}

sub1()
{
    sleep 5
    echo "sub1 done"
}

main()
{
    local split="=================================================="
    echo $split

    local type=A
    func_${type}
    echo $split

    echo $1
    echo $2
    v="main"
    echo "main: v=$v"
    cv=100
    echo "main: cv=$cv"
    func
    echo "main: v=$v"
    echo "main: cv=$cv"
    echo $split

    sub1 &
    pid1=$!
    echo "sub1 pid: $pid1"

    # 不能写成一行
    {
        sleep 5
        echo "sub2 done"
    } &
    pid2=$!
    echo "sub2 pid: $pid2"

    jobs -p
    wait $pid1 $pid2
    echo $split
}

v="global"
cv=1

main $@
exit $?
